/*
 * StateManager.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.toolkit;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.JLabel;
import javax.swing.Timer;
import org.tiberiumlabs.lailaps.Mediator;
import org.tiberiumlabs.lailaps.gui.GUIBuilder;
import static org.tiberiumlabs.lailaps.gui.elements.AbstractStatusBar.BOLD_FONT_10;
import static org.tiberiumlabs.lailaps.gui.elements.AbstractStatusBar.BOLD_FONT_11;
import static org.tiberiumlabs.lailaps.gui.elements.AbstractStatusBar.PLAIN_FONT_10;
import static org.tiberiumlabs.lailaps.gui.elements.AbstractStatusBar.PLAIN_FONT_11;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public abstract class StateManager {

    public static final int STOPPED = 1;
    public static final int WORKING = 2;
    public static final int COMPLETED = 3;
    
    private final ResourceBundle bundle = ResourceBundle.getBundle("org/tiberiumlabs/lailaps/resources/messages/statusbar");
    private final String totalTooltip = bundle.getString("total.tooltip");
    private final String stoppedTooltip = bundle.getString("stopped.tooltip");
    private final String workingTooltip = bundle.getString("working.tooltip");
    private final String completedTooltip = bundle.getString("completed.tooltip");

    public StateManager() {
        totalTimer.setRepeats(false);
        stoppedTimer.setRepeats(false);
        workingTimer.setRepeats(false);
        completedTimer.setRepeats(false);
    }
    
    // <editor-fold defaultstate="collapsed" desc=" bean ">
    private int total = 0;
    private int working = 0;
    private int stopped = 0;
    private int completed = 0;

    public int getTotal() {
        return total;
    }

    public int getWorking() {
        return working;
    }

    public int getStopped() {
        return stopped;
    }

    public int getCompleted() {
        return completed;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc=" changers ">
    public void add(int status, int count) {
        if (count == 0) {
            return;
        }
        switch (status) {
            case STOPPED:
                {
                    stopped = stopped + count;
                    updateStopped();
                    break;
                }
            case WORKING:
                {
                    working = working + count;
                    updateWorking();
                    break;
                }
            case COMPLETED:
                {
                    completed = completed + count;
                    updateCompleted();
                    break;
                }
        }
        total = total + count;
        updateTotal();
    }

    public void remove(int status, int count) {
        if (count == 0) {
            return;
        }
        switch (status) {
            case STOPPED:
                {
                    stopped = stopped - count;
                    updateStopped();
                    break;
                }
            case WORKING:
                {
                    working = working - count;
                    updateWorking();
                    break;
                }
            case COMPLETED:
                {
                    completed = completed - count;
                    updateCompleted();
                    break;
                }
        }
        total = total - count;
        updateTotal();
    }

    public void change(int oldStatus, int newStatus, int count) {
        if (count == 0 || oldStatus == newStatus) {
            return;
        }
        remove(oldStatus, count);
        add(newStatus, count);
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" gui updaters ">
    private GUIBuilder guiBuilder = Mediator.getMediator().getGuiBuilder();

    private void updateTotal() {
        setTotal(true, true);
        totalTimer.restart();
    }

    private void updateStopped() {
        setStopped(true, true);
        stoppedTimer.restart();
    }

    private void updateWorking() {
        setWorking(true, true);
        workingTimer.restart();
    }

    private void updateCompleted() {
        setCompleted(true, true);
        completedTimer.restart();
    }

    private void setTotal(boolean tooltipChandeg, boolean isBold) {
        String tooltip = tooltipChandeg ? totalTooltip + ' ' + total : null;
        String value = String.valueOf(total);
        if (guiBuilder.isMainWindowVisible()) {
            JLabel label = guiBuilder.getMainStatusBar().getTotalLabel();
            label.setText(value);
            if (isBold) {
                label.setFont(BOLD_FONT_11);
            } else {
                label.setFont(PLAIN_FONT_11);
            }
            if (tooltipChandeg) {
                label.setToolTipText(tooltip);
            }
        }
        if (guiBuilder.isDropTargetVisible()) {
            JLabel label = guiBuilder.getDropTargetStatusBar().getTotalLabel();
            label.setText(value);
            if (isBold) {
                label.setFont(BOLD_FONT_10);
            } else {
                label.setFont(PLAIN_FONT_10);
            }
            if (tooltipChandeg) {
                label.setToolTipText(tooltip);
            }
        }
    }

    private void setStopped(boolean tooltipChandeg, boolean isBold) {
        String tooltip = tooltipChandeg ? stoppedTooltip + ' ' + stopped : null;
        String value = String.valueOf(stopped);
        if (guiBuilder.isMainWindowVisible()) {
            JLabel label = guiBuilder.getMainStatusBar().getStoppedLabel();
            label.setText(value);
            if (isBold) {
                label.setFont(BOLD_FONT_11);
            } else {
                label.setFont(PLAIN_FONT_11);
            }
            if (tooltipChandeg) {
                label.setToolTipText(tooltip);
            }
        }
        if (guiBuilder.isDropTargetVisible()) {
            JLabel label = guiBuilder.getDropTargetStatusBar().getStoppedLabel();
            label.setText(value);
            if (isBold) {
                label.setFont(BOLD_FONT_10);
            } else {
                label.setFont(PLAIN_FONT_10);
            }
            if (tooltipChandeg) {
                label.setToolTipText(tooltip);
            }
        }
    }

    private void setWorking(boolean tooltipChandeg, boolean isBold) {
        String tooltip = tooltipChandeg ? workingTooltip + ' ' + working : null;
        String value = String.valueOf(working);
        if (guiBuilder.isMainWindowVisible()) {
            JLabel label = guiBuilder.getMainStatusBar().getWorkingLabel();
            label.setText(value);
            if (isBold) {
                label.setFont(BOLD_FONT_11);
            } else {
                label.setFont(PLAIN_FONT_11);
            }
            if (tooltipChandeg) {
                label.setToolTipText(tooltip);
            }
        }
        if (guiBuilder.isDropTargetVisible()) {
            JLabel label = guiBuilder.getDropTargetStatusBar().getWorkingLabel();
            label.setText(value);
            if (isBold) {
                label.setFont(BOLD_FONT_10);
            } else {
                label.setFont(PLAIN_FONT_10);
            }
            if (tooltipChandeg) {
                label.setToolTipText(tooltip);
            }
        }
    }

    private void setCompleted(boolean tooltipChandeg, boolean isBold) {
        String tooltip = tooltipChandeg ? completedTooltip + ' ' + completed : null;
        String value = String.valueOf(completed);
        if (guiBuilder.isMainWindowVisible()) {
            JLabel label = guiBuilder.getMainStatusBar().getCompletedLabel();
            label.setText(value);
            if (isBold) {
                label.setFont(BOLD_FONT_11);
            } else {
                label.setFont(PLAIN_FONT_11);
            }
            if (tooltipChandeg) {
                label.setToolTipText(tooltip);
            }
        }
        if (guiBuilder.isDropTargetVisible()) {
            JLabel label = guiBuilder.getDropTargetStatusBar().getCompletedLabel();
            label.setText(value);
            if (isBold) {
                label.setFont(BOLD_FONT_10);
            } else {
                label.setFont(PLAIN_FONT_10);
            }
            if (tooltipChandeg) {
                label.setToolTipText(tooltip);
            }
        }
    }
    private int delay = 1000;
    
    private final Timer totalTimer = new Timer(delay, new TotalTimerActionListener());
    private final Timer stoppedTimer = new Timer(delay, new StoppedTimerActionListener());
    private final Timer workingTimer = new Timer(delay, new WorkingTimerActionListener());
    private final Timer completedTimer = new Timer(delay, new CompletedTimerActionListener());

    private class TotalTimerActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            setTotal(false, false);
        }
    }

    private class StoppedTimerActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            setStopped(false, false);
        }
    }

    private class WorkingTimerActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            setWorking(false, false);
        }
    }

    private class CompletedTimerActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            setCompleted(false, false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" histogram helper ">
    int br; //buffer register

    /**
     * Increment more data to currnet time period.
     * @param size new incremental value
     */
    public void incrementDownloadedValue(int size) {
        br += size;
    }

    /**
     * Reset collected value.
     */
    public void resetDownloadedValue() {
        br = 0;
    }

    public int getDownloadedValue() {
        return br;
    }
    // </editor-fold>
}